<?php
// 引入配置文件
include_once('./config/config.php');
$admin = AuthCheck();
$action = $_GET['action'] ?? '';
// 退出账号
if ($action === 'logout') {
  setcookie('auth', '', time() - 1);
  notice('退出成功', 'login.php');
}

// 判断是否为post提交
if ($_POST) {
  // 接收参数
  $number = $_POST['number'] ? trim($_POST['number']) : '';
  $name = $_POST['name'] ? trim($_POST['name']) : '';
  $age = $_POST['age'] ? trim($_POST['age']) : '';
  $gender = $_POST['gender'] ? trim($_POST['gender']) : '';
  $depid = $_POST['dep'] ? trim($_POST['dep']) : '';

  if (empty($number)) {
    notice('学号不能为空');
  }
  if (empty($name)) {
    notice('姓名不能为空');
  }
  if (empty($age)) {
    notice('年龄不能为空');
  }
  if (!$gender) {
    notice('请选择性别');
  }
  if (!$depid) {
    notice('请选择部门');
  }
  // 调用封装上传函数
  $avatar = build_upload('avatar');

  if ($avatar['code'] === 0) {
    notice($avatar['msg']);
  }
  // 组装参数
  $data = [
    'number' => +$number,
    'avatar' => $avatar['data'],
    'name' => $name,
    'gender' => $gender,
    'age' => +$age,
    'depid' => $depid,
    'createtime' => +time()
  ];

  $result = add('student', $data);
  if ($result['code'] === 1) {
    notice('添加学生成功', 'StudentList.php');
  } else {
    is_file('.' . $avatar) &&  unlink('.' . $avatar);
    notice('添加学生失败，请重试', 'StudentAdd.php');
    exit;
  }
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('./common/meta.php'); ?>
  <style>
    #upload {
      width: 214px;
      height: 200px;
      object-fit: cover;
      cursor: pointer;
    }
  </style>

</head>


<body>
  <!-- 公共头部 -->
  <?php include_once('./common/header.php'); ?>
  <!-- 公共菜单栏 -->
  <?php include_once('./common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加学生</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="StudentList.php">StudentList</a> <span class="divider">/</span></li>
      <li class="active">StudentAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='StudentList.php'"><i class="icon-list"></i> 学生列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <label>学号</label>
                <input type="text" value="" name="number" id="number" class="input-xxlarge">
                <label>姓名</label>
                <input type="text" value="" name="name" id="name" class="input-xxlarge">
                <label>年龄</label>
                <input type="text" value="" name="age" id="age" class="input-xxlarge">
                <label>性别</label>
                <select name="gender" id="gender">
                  <option value="0">保密</option>
                  <option value="1">男</option>
                  <option value="2">女</option>
                </select>
                <label>部门</label>
                <select name="dep" id="dep">
                  <option value="0">请选择</option>
                </select>
                <label>头像</label>
                <input type="file" name="avatar" id="avatar" hidden class="input-xxlarge">
                <img src="./assets/images/upload.png" id="upload" alt="">
                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <!-- 公共底部 -->
        <?php include_once('./common/footer.php'); ?>

      </div>
    </div>
  </div>

</body>

</html>
<!-- 公共js -->
<?php include_once('./common/script.php'); ?>
<script>
  // 图片上传
  $('#upload').click(function() {
    $('#avatar').click();
  })

  $('#avatar').change(function() {
    let file = this.files[0] ?? '';
    if (file) {
      let Reader = new FileReader();
      Reader.readAsDataURL(file);
      Reader.onload = function() {
        $('#upload').attr('src', Reader.result);
      }
    }
  });

  // 获取部门
  (() => {
    $.ajax({
      type: 'post',
      url: 'api.php',
      data: {
        'action': 'select_department'
      },
      dataType: 'json',
      success: res => {
        console.log(res)
        if (res.code === 1) {
          const data = res.data
          let temp = ''
          data.forEach(item => {
            temp += `<option value="${item.id}">${item.name}</option>`
          })
          $('#dep').append(temp)
        } else {
          alert(res.msg)
        }
      }
    })
  })()
</script>